package com.idc.excel.dsw;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;

public class ReadFormulaAndDoAsync {

	static {
		((LoggerContext) LoggerFactory.getILoggerFactory()).getLoggerList()
		.forEach(logger -> logger.setLevel(Level.INFO));
		System.setProperty("entityExpansionLimit", "300000000");
		System.setProperty("totalEntitySizeLimit", "300000000");
		System.setProperty("jdk.xml.totalEntitySizeLimit", "300000000");
	}
	
	public static void readAddFormula() {
		long a = System.currentTimeMillis();
		List<DswData> dataList = new ArrayList<>();
		// Map<String, List<DswData>> dataMap = new LinkedHashMap<String, List<DswData>>();
		String filePath = "C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\CR\\originData.xlsx";
		String writePath = "C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\CR\\new\\";
		File writeFile = new File(writePath);
		if (!writeFile.exists()) {
			writeFile.mkdirs();
		}
		AtomicInteger n = new AtomicInteger();
		EasyExcel.read(filePath, DswReadData.class, new ReadListener<DswReadData>() {
			@Override
			public void invoke(DswReadData data, AnalysisContext context) {
				if (StringUtils.isEmpty(data.getProvinceName())) {
					return;
				}
				int total = n.incrementAndGet();
				if (total % 10000 == 0) {
					System.out.println("done: " + total);
				}
				dataList.add(JSON.parseObject(JSON.toJSONString(data), DswData.class));
			}

			@Override
			public void doAfterAllAnalysed(AnalysisContext context) {

			}
		}).sheet(0).doRead();
		write(dataList, writePath + "dsw-out.xlsx");
		System.out.println("done:" + dataList.size() + " cost:" + (System.currentTimeMillis() - a));
	}

	public static void exportProvinceData() {
		Map<String, List<DswData>> dataMap = new LinkedHashMap<String, List<DswData>>();
		AtomicInteger n = new AtomicInteger();
		EasyExcel.read("C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\CR\\new\\dsw-out.xlsx",
				DswData.class,
				new ReadListener<DswData>() {

			@Override
			public void invoke(DswData data, AnalysisContext context) {
				int total = n.incrementAndGet();
				if (total % 10000 == 0) {
					System.out.println("done: " + total);
				}
				RulesUtil.fixData(data);
				String provinceName = data.getProvinceName();
				List<DswData> cds = dataMap.get(provinceName);
				if (null == cds) {
					cds = new ArrayList<>();
					dataMap.put(provinceName, cds);
				}
				cds.add(data);
			}

			@Override
			public void doAfterAllAnalysed(AnalysisContext context) {
				
			}
		}).sheet(0).doRead();
		dataMap.forEach((k, v) -> {
			EasyExcel.write(
					"C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\CR\\province\\dsw\\" + k + ".xlsx",
					DswData.class).sheet("Sheet1").doWrite(v);
		});
	}

	public static void write(List<DswData> dataList, String writePath) {
		ExcelWriter excelWriter = EasyExcel.write(writePath, DswData.class).registerWriteHandler(new DswCellWriteHandler()).build();
		// 创建Sheet对象
		WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
		excelWriter.write(dataList, writeSheet);
		// Workbook workbook = excelWriter.writeContext().writeWorkbookHolder().getWorkbook();
		// FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
		// evaluator.evaluateAll(); // 全表公式计算
		excelWriter.finish();
	}

	public static void main(String[] args) {
		// readAddFormula();
		exportProvinceData();
	}
}